<style scoped lang="scss">
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
.detail{
  height: 100%;
  width: 100%;
  display: flex;
  gap: 30px;
  .title{
    font-size: 25px;
    font-weight: 600;
  }
  .data{
    display: flex;
    gap: 15px;
    >div{
      display: flex;
      align-items: center;
      gap: 3px;
      color: rgb(119, 126, 139);
    }
  }
  .left{
    overflow: auto;
    height: 100%;
    &::-webkit-scrollbar{
      display: none;
    }
    flex: .4;
  }
  .right{
    flex: 2;
    height: 100%;
    overflow: scroll;
    .comment{
      margin-top: 30px;
    }
  }
}
</style>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" v-if="false">
      <el-form-item label="分类名称" prop="categoryType">
        <el-select v-model="queryParams.articleType" style="width: 200px">
          <el-option
            v-for="item in typeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.articleType"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文章标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入文章标题"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="数据状态" clearable style="width: 200px">
          <el-option
            label="草稿"
            value="1"
          />
          <el-option
            label="审核中"
            value="2"
          />
          <el-option
            label="审核未通过"
            value="3"
          />
          <el-option
            label="已发布"
            value="4"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="false">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:article-category:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:article-category:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5" v-if="false">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:article-category:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Close"-->
<!--          @click="handleClose"-->
<!--        >关闭</el-button>-->
<!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文章编码" align="center" prop="id" />
      <el-table-column label="文章标题" align="center" prop="title" />
      <el-table-column label="文章封面" align="center" prop="dictCode">
        <el-image
          style="width: 100px; height: 100px"
          :src="url"
          :preview-src-list="srcList">
        </el-image>
      </el-table-column>
      <el-table-column label="文章摘要" align="center" prop="articleAbstract" />
      <el-table-column label="作者" align="center" prop="userName" />
      <el-table-column label="提交时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width" >
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleUpdate(scope.row)">预览</el-button>
          <el-button link type="primary" icon="Check" @click="accessArticle(scope.row,true)">通过</el-button>
          <el-button link type="primary" icon="Close" @click="accessArticle(scope.row,false)">不通过</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
<!--    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>-->
<!--      <MdCatalog :editorId="id"-->
<!--                 :scrollElement="scrollElement" />-->
<!--      &lt;!&ndash;      内容&ndash;&gt;-->
<!--      <MdPreview :id="id"-->
<!--                 :modelValue="text" />-->
<!--    </el-dialog>-->

    <el-dialog
      v-model="open"
      fullscreen
      top="40vh"
      width="70%"
      draggable
    >
      <div class="detail">
<!--        <div class="left">-->
<!--&lt;!&ndash;            <el-card shadow="always">&ndash;&gt;-->
<!--              <MdCatalog :editorId="id"-->
<!--                         :scrollElement="scrollElement" />-->
<!--&lt;!&ndash;            </el-card>&ndash;&gt;-->
<!--        </div>-->
<!--        <div class="right">-->
<!--          <el-card style="height: 100%;width: 100%;overflow: auto" class="content">-->
<!--              <h1 class="title">-->
<!--                {{ article.title }}-->
<!--              </h1>-->

            <!--      内容-->
            <MdPreview :id="id"
                       :modelValue="text" />
            <!--        <a-spin v-show="!text" size="large"></a-spin>-->
            <!--        评论-->
<!--          </el-card>-->
<!--        </div>-->

      </div>
<!--      <template #footer>-->
<!--        <div class="dialog-footer">-->
<!--          <el-button @click="open = false">返回</el-button>-->
<!--        </div>-->
<!--      </template>-->
    </el-dialog>
  </div>
</template>

<script setup name="Data">
import {MdCatalog,MdPreview} from 'md-editor-v3';
import useDictStore from '@/store/modules/dict'
import { optionselect as getDictOptionselect, getType } from "@/api/client/article-category/type";
import { listData, getData, delData, addData, updateData } from "@/api/client/article-category/data";
import {getCurrentInstance, ref} from "vue";
import {useRoute} from "vue-router";
import {ElMessage} from "element-plus";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");


const url = 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'
const srcList = [
  'https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg',
  'https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg'
]
const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const defaultArticleType = ref("");
const typeOptions = ref([]);
const route = useRoute();

// 数据分类回显样式
const listClassOptions = ref([
  { value: "default", label: "默认" },
  { value: "primary", label: "主要" },
  { value: "success", label: "成功" },
  { value: "info", label: "信息" },
  { value: "warning", label: "警告" },
  { value: "danger", label: "危险" }
]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    params: {},
    title: undefined,
    articleType: undefined,
    status: 2
  },
  rules: {
    coverImg: [{ required: true, message: "文章封面不能为空", trigger: "blur" }],
    isPublic: [{ required: true, message: "请选择是否公开", trigger: "blur" }],
    customHot: [{ required: true, message: "请选择是否热门", trigger: "blur" }],
    status: [{ required: true, message: "请选择状态", trigger: "blur" }],
    articleAbstract: [{ required: true, message: "文章摘要不能为空", trigger: "blur" }]
  }
});

const { queryParams, form, rules } = toRefs(data);
const id = 'preview-only';
const text = ref();
const scrollElement = '.detail .right .ant-card'
const e = document.querySelector('.right .content .ant-card-body')
/** 查询分类类型详细 */
function getTypes(id) {
  getType(id).then(response => {
    // queryParams.value.articleType = response.data.articleType;
    defaultArticleType.value = response.data.articleType;
    getList();
  });
}

/** 查询分类类型列表 */
function getTypeList() {
  getDictOptionselect().then(response => {
    typeOptions.value = response.data;
  });
}
/** 查询分类数据列表 */
function getList() {
  loading.value = true;
  queryParams.value.params['category_id'] = route.params.categoryId
  if (queryParams.value.articleType) queryParams.value.params['article_type'] = queryParams.value.articleType
  if (queryParams.value.title) queryParams.value.params['title'] = queryParams.value.title
  if (queryParams.value.status) queryParams.value.params['status'] = queryParams.value.status
  listData(queryParams.value).then(response => {
    dataList.value = response.rows
    total.value = response.total
    queryParams.value.params = {}
    loading.value = false;
  });
}
getList();
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    dictCode: undefined,
    dictLabel: undefined,
    dictValue: undefined,
    cssClass: undefined,
    listClass: "default",
    dictSort: 0,
    status: "1",
    remark: undefined
  };
  proxy.resetForm("dataRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 返回按钮操作 */
function handleClose() {
  const obj = { path: "/client/article-category" };
  proxy.$tab.closeOpenPage(obj);
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.articleType = defaultArticleType.value;
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加分类数据";
  form.value.articleType = queryParams.value.articleType;
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.dictCode);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getData(id).then(response => {
    form.value = response.data;
    text.value = form.value.content
    open.value = true;
    title.value = "修改分类数据";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["dataRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateData(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addData(form.value).then(response => {
          useDictStore().removeDict(queryParams.value.articleType);
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除分类编码为"' + dictCodes + '"的数据项？').then(function() {
    return delData(ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
    useDictStore().removeDict(queryParams.value.articleType);
  }).catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/dict/data/export", {
    ...queryParams.value
  }, `dict_data_${new Date().getTime()}.xlsx`);
}
const accessArticle = async (row, isAccess) => {
  const result = await updateData({id:row.id,status:isAccess ? 4 : 3})
  if (result.code == 200) {
    if (isAccess) ElMessage.success('审核通过')
    else ElMessage.success('审核不通过')
    getList()
  }
}
// getTypes(route.params && route.params.categoryId);
// getTypeList();
</script>
